<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/8
 * Time: 10:29 AM
 */
namespace app\index\controller;
use think\Exception;
class Report extends CoinBaseContorller
{
    /**
     *
     * @api {post} api/report/PurchaseIn  web_rep_充值记录
     * @apiDescription   web_rep_充值记录
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} begindate 其实日期
     * @apiParam {string} enddate 结束日期
     * @apiParam {string} phone 手机号
     * @apiParam {string} coinguid 币种 默认 USDT
     *
     * @apiSuccess {string}  guid  内码
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  coinname  币名
     * @apiSuccess {string}  billdatetime  交易日期
     * @apiSuccess {string}  status 状态  -1 失败   0已提交  1 成功
     * @apiSuccess {string}  purchasein_address 源地址
     * @apiSuccess {string}  sellout_address 目标地址
     * @apiSuccess {string}  tokencount  数量
     * @apiSuccess {string}  fee  手续费
     * @apiSuccess {string}  transaction_id  交易ID
     * @apiSuccess {string}  message 备注
     * @apiVersion 1.0.1
     */
   public function PurchaseIn(){
       $page=$this->param("page");
       $count=$this->param("count");
       $begindate=$this->param("begindate");
       $enddate=$this->param("enddate");
       $phone=$this->param("phone");
       $coinguid=$this->param("coinguid");

       $sql="select a.guid,b.phone,c.name coinname,a.billdatetime,a.status,a.tokencount/power(10,8) tokencount,a.fee/power(10,8) fee,a.transaction_id,a.message,a.purchasein_address,a.sellout_address from token_bill_transaction_coin a
inner join token_sys_user b on a.token_sys_user_guid =b.guid INNER JOIN token_sys_coin c on c.guid=a.token_sys_coin_guid 
WHERE a.type=0 and a.billdate between '".$begindate."' and '".$enddate."' and a.token_sys_coin_guid ='".$coinguid."' and [选择条件] ORDER BY a.billdatetime desc ";
       if(strlen($phone)>0){
           $sql=str_replace("[选择条件]"," b.phone='".$phone."'",$sql);
       }
       $sql=str_replace("[选择条件]"," 1=1 ",$sql);
       $result =$this->SetReportPagination($sql,$page,$count);
       return $this->json_data($result,"");
   }



    /**
     *
     * @api {post} api/report/Web_ExChange_List  web_rep_兑换记录
     * @apiDescription   web_rep_兑换记录
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} begindate 其实日期
     * @apiParam {string} enddate 结束日期
     * @apiParam {string} phone 手机号
     *
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  srccoinname  源币名
     * @apiSuccess {string}  tarcoinname  目标币名
     * @apiSuccess {string}  billdatetime  交易日期
     * @apiSuccess {string}  putin_token_count  交易数量
     * @apiSuccess {string}  fee  手续费比例
     * @apiSuccess {string}  result_token_count  最终数量
     * @apiSuccess {string}  fee_token_count  获得的手续费数量
     * @apiSuccess {string}  sum_token_count  兑换总数量
     * @apiVersion 1.0.1
     */
    public function Web_ExChange_List(){
        $page=$this->param("page");
        $count=$this->param("count");
        $begindate=$this->param("begindate");
        $enddate=$this->param("enddate");
        $phone=$this->param("phone");

        $sql="select a.guid,b.phone,c.name srccoinname,d.name tarcoinname,a.result_token_count/power(10,8) result_token_count,a.billdatetime,a.putin_token_count/power(10,8) putin_token_count,a.fee,a.fee_token_count/power(10,8) fee_token_count,a.sum_token_count/power(10,8) sum_token_count from token_bill_exchange_coin a inner join token_sys_user b on a.token_sys_user_guid =b.guid
INNER JOIN token_sys_coin c on c.guid=a.src_token_sys_coin_guid INNER JOIN token_sys_coin d on d.guid=a.target_token_sys_coin_guid
WHERE  a.billdate between '".$begindate."' and '".$enddate."'  and [选择条件] ORDER BY a.billdatetime desc ";
        if(strlen($phone)>0){
            $sql=str_replace("[选择条件]"," b.phone='".$phone."'",$sql);
        }
        $sql=str_replace("[选择条件]"," 1=1 ",$sql);
        $result =$this->SetReportPagination($sql,$page,$count);
        return $this->json_data($result,"");
    }




    /**
     *
     * @api {post} api/report/AppPurchaseIn  App购买记录
     * @apiDescription   App购买记录
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  coinname  币名
     * @apiSuccess {string}  billdatetime  交易日期
     * @apiSuccess {string}  tokencount  数量
     * @apiSuccess {string}  fee  手续费
     * @apiSuccess {string}  transaction_id  交易ID
     * @apiSuccess {string}  status  状态码
     * @apiSuccess {string}  statustitle  状态信息
     * @apiVersion 1.0.1
     */
    public function AppPurchaseIn(){
        $page=$this->param("page");
        $count=$this->param("count");
        $userid=$this->getuserGuid();
        $sql="select a.guid,b.phone,c.name coinname,a.billdatetime,a.tokencount/power(10,8) tokencount,a.fee/power(10,8) fee,a.transaction_id,CASE a.status WHEN -1 THEN '购买失败' WHEN 1 THEN '购买成功' ELSE '正在审核' END statustitle,a.status from token_bill_transaction_coin a inner join token_sys_user b on a.token_sys_user_guid =b.guid INNER JOIN token_sys_coin c on c.guid=a.token_sys_coin_guid 
WHERE a.type=0 and a.token_sys_user_guid='".$userid."' ORDER BY a.billdatetime desc LIMIT ";
        $sql=$sql.($page-1)*$count.",".$count."";
        return $this->json_data($this->db_select($sql),"");
    }

    /**
     *
     * @api {post} api/report/SellOut  web提币
     * @apiDescription   web提币
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} begindate 其实日期
     * @apiParam {string} enddate 结束日期
     * @apiParam {string} phone 手机号
     * @apiParam {string} status 状态   默认 已提交
     * @apiParam {string} coinguid 币种 默认 USDT
     *
     * @apiSuccess {string}  guid  内码
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  coinname  币名
     * @apiSuccess {string}  billdatetime  交易日期
     * @apiSuccess {string}  status 状态  -1 失败   0已提交  1 成功  2 已审核
     * @apiSuccess {string}  purchasein_address 源地址
     * @apiSuccess {string}  sellout_address 目标地址
     * @apiSuccess {string}  tokencount  数量
     * @apiSuccess {string}  fee  手续费
     * @apiSuccess {string}  transaction_id  交易ID
     * @apiSuccess {string}  message 备注
     * @apiVersion 1.0.1
     */
    public function SellOut(){
        $page=$this->param("page");
        $count=$this->param("count");
        $begindate=$this->param("begindate");
        $enddate=$this->param("enddate");
        $phone=$this->param("phone");
        $status=$this->param("status");
        $coinguid=$this->param("coinguid");

        $sql="select a.guid,b.phone,c.name coinname,a.billdatetime,a.status,a.tokencount/power(10,8) tokencount,a.fee/power(10,8) fee,a.transaction_id,a.message,a.purchasein_address,a.sellout_address from token_bill_transaction_coin a
inner join token_sys_user b on a.token_sys_user_guid =b.guid INNER JOIN token_sys_coin c on c.guid=a.token_sys_coin_guid 
WHERE a.type=1 and a.billdate between '".$begindate."' and '".$enddate."' and a.token_sys_coin_guid ='".$coinguid."' [选择条件] ORDER BY a.billdatetime desc ";
        if(strlen($phone)>0){
            $sql=str_replace("[选择条件]"," [选择条件] and b.phone='".$phone."'",$sql);
        }
        if(strlen($status)>0){
            $sql=str_replace("[选择条件]"," [选择条件] and a.status=".$status."",$sql);
        }
        $sql=str_replace("[选择条件]"," and 1=1 ",$sql);
        $result =$this->SetReportPagination($sql,$page,$count);
        return $this->json_data($result,"");
    }

    /**
     *
     * @api {post} api/report/AppSellOut  app转出记录列表
     * @apiDescription   app转出记录
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} coinguid 币guid
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  coinname  币名
     * @apiSuccess {string}  billdatetime  交易日期
     * @apiSuccess {string}  tokencount  数量
     * @apiSuccess {string}  fee  手续费
     * @apiSuccess {string}  transaction_id  交易ID
     * @apiSuccess {string}  type  0 转入 1 转出
     * @apiVersion 1.0.1
     */
    public function AppSellOut(){
        $page=$this->param("page");
        $count=$this->param("count");
        $coinguid=$this->param("coinguid");
        $uesrid=$this->getuserGuid();
        $sql="select a.guid,b.phone,c.name coinname,a.billdatetime,a.tokencount/power(10,8) tokencount,a.fee/power(10,8) fee,a.transaction_id,a.type,a.status,case a.status when 1 then '已成功' when 0 then '审核中' else '失败' end statustitle from token_bill_transaction_coin a inner join token_sys_user b on a.token_sys_user_guid =b.guid INNER JOIN token_sys_coin c on c.guid=a.token_sys_coin_guid 
WHERE a.token_sys_coin_guid ='".$coinguid."' and a.token_sys_user_guid='".$uesrid."' ORDER BY a.billdatetime desc LIMIT ";
        $sql=$sql.($page-1)*$count.",".$count."";
        return $this->json_data($this->db_select($sql),"");
    }


    /**
     *
     * @api {post} api/report/RefusedSellOut 提币驳回
     * @apiDescription   提币驳回
     * @apiGroup report
     * @apiParam {string} guid guid
     * @apiSuccess {string}  data  为空成功
     * @apiVersion 1.0.1
     */
    public function RefusedSellOut(){
        $guid=$this->param("guid");
        try{
            $this->_RefusedSellOut($guid);
            return  $this->json_data("","");
        }catch (Exception $e){
            $this->json_data("",$e->getMessage());
        }
    }

    public  function _RefusedSellOut($guid){
        $row=$this->db_select("SELECT status,citpay_billno,token_sys_coin_guid,tokencount,token_sys_user_guid from token_bill_transaction_coin where guid='".$guid."'");
        if($row[0]["status"]!=0){
           exception("只有已提交,才能驳回");
           return;
        }
        $billno=$row[0]["citpay_billno"];
        $api=new CitPayApi();
        $api->ReturnSellout(-1,$billno);
    }


    /**
     *
     * @api {post} api/report/Exchange  APP兑换记录
     * @apiDescription   APP兑换记录
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} src_coin_guid  源币GUID
     * @apiParam {string} tart_coin_guid 目标GUID
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  srccoinname  源币名
     * @apiSuccess {string}  tarcoinname  目标币名
     * @apiSuccess {string}  billdatetime  交易日期
     * @apiSuccess {string}  putin_token_count  交易数量
     * @apiSuccess {string}  fee  手续费比例
     * @apiSuccess {string}  result_token_count  最终数量
     * @apiSuccess {string}  fee_token_count  获得的手续费数量
     * @apiSuccess {string}  sum_token_count  兑换总数量
     * @apiSuccess {string}  ratio  兑换比率1:N
     * @apiVersion 1.0.1
     */
    public function Exchange(){
        $src_coin_guid=$this->param("src_coin_guid");
        $tart_coin_guid=$this->param("tart_coin_guid");
        $page=$this->param("page");
        $count=$this->param("count");
        $userid=$this->getuserGuid();
        $sql="select a.guid,b.phone,c.name srccoinname,d.name tarcoinname,a.result_token_count/power(10,8) result_token_count,a.billdatetime,a.putin_token_count/power(10,8) putin_token_count,a.fee,a.fee_token_count/power(10,8) fee_token_count,a.sum_token_count/power(10,8) sum_token_count,ratio  from token_bill_exchange_coin a inner join token_sys_user b on a.token_sys_user_guid =b.guid
INNER JOIN token_sys_coin c on c.guid=a.src_token_sys_coin_guid INNER JOIN token_sys_coin d on d.guid=a.target_token_sys_coin_guid
WHERE a.src_token_sys_coin_guid='".$src_coin_guid."' and a.target_token_sys_coin_guid='".$tart_coin_guid."' and a.token_sys_user_guid='".$userid."' ORDER BY a.billdatetime desc LIMIT ";
        $sql=$sql.($page-1)*$count.",".$count."";
        return $this->json_data($this->db_select($sql),"");
    }


    /**
     *
     * @api {post} api/report/Earnings  web_rep_收益
     * @apiDescription   web_rep_收益记录
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} begindate 其实日期
     * @apiParam {string} enddate 结束日期
     * @apiParam {string} phone 手机号
     *
     * @apiSuccess {string}  phone  手机号
     * @apiSuccess {string}  billdate  日期
     * @apiSuccess {string}  rolename  角色名称
     * @apiSuccess {string}  earnings_tokencount  总收益
     * @apiSuccess {string}  sys_earnings_tokencount  系统收益
     * @apiSuccess {string}  user_earnings_tokencount 好友收益
     * @apiVersion 1.0.1
     */
    public function Earnings(){
        $page=$this->param("page");
        $count=$this->param("count");
        $begindate=$this->param("begindate");
        $enddate=$this->param("enddate");
        $phone=$this->param("phone");

        $sql="select  a.phone,a.billdate,a.earnings_tokencount,a.rolename,a.sys_earnings_tokencount,a.user_earnings_tokencount from ( select  b.phone,a.billdate,sum(a.earnings_tokencount)/power(10,8) earnings_tokencount,c.name rolename,d.earnings_tokencount/power(10,8) sys_earnings_tokencount,ifnull(e.earnings_tokencount/power(10,8),0) user_earnings_tokencount from  token_bill_earnings a inner join token_sys_user b on a.token_sys_user_guid =b.guid
INNER JOIN token_sys_role c on c.guid=b.token_sys_role_guid  INNER JOIN (SELECT token_sys_user_guid,billdate,sum_tokencount,earnings_tokencount from token_bill_earnings WHERE type=0) d on d.token_sys_user_guid=a.token_sys_user_guid and d.billdate=a.billdate
LEFT JOIN (SELECT token_sys_user_guid,billdate,sum(earnings_tokencount) earnings_tokencount from token_bill_earnings WHERE type=1
GROUP BY  token_sys_user_guid,billdate ) e on e.token_sys_user_guid=a.token_sys_user_guid and e.billdate=a.billdate WHERE a.billdate BETWEEN '".$begindate."' and '".$enddate."' and [选择条件]   
GROUP BY  b.phone,a.billdate,c.name,d.earnings_tokencount,e.earnings_tokencount ORDER BY a.billdate desc) a ORDER BY a.billdate DESC,a.earnings_tokencount DESC";
        if(strlen($phone)>0){
            $sql=str_replace("[选择条件]"," b.phone like '".$phone."%'",$sql);
        }
        $sql=str_replace("[选择条件]"," 1=1 ",$sql);
        $result =$this->SetReportPagination($sql,$page,$count);
        return $this->json_data($result,"");
    }


    /**
     *
     * @api {post} api/report/EarningsSysDetail  web_rep_收益_系统
     * @apiDescription   web_rep_系统收益
     * @apiGroup report
     * @apiParam {string} billdate 日期
     * @apiParam {string} phone 手机号
     *
     * @apiSuccess {string}  sum_tokencount  本金
     * @apiSuccess {string}  ratio  利率
     * @apiSuccess {string}  earnings_tokencount  系统收益
     * @apiVersion 1.0.1
     */
    public function EarningsSysDetail(){
        $phone=$this->param("phone");
        $billdate=$this->param("billdate");
        $sql="select a.sum_tokencount/power(10,8) sum_tokencount,b.ratio,a.earnings_tokencount/power(10,8) earnings_tokencount from token_bill_earnings a INNER JOIN token_sys_earnings_detail b on b.guid=a.token_sys_earnings_detail_guid  inner join token_sys_user c on a.token_sys_user_guid =c.guid WHERE a.type=0 and c.phone='".$phone."' and a.billdate='".$billdate."'";
        $result=$this->db_select($sql);
        return $this->json_data($result,"");
    }


    /**
     *
     * @api {post} api/report/EarningsUserSum  web_rep_收益_用户汇总
     * @apiDescription   web_rep_用户收益汇总
     * @apiGroup report
     * @apiParam {string} billdate 日期
     * @apiParam {string} phone 手机号
     *
     * @apiSuccess {string}  guid  guid
     * @apiSuccess {string}  counts  总人数
     * @apiSuccess {string}  rolename  角色名称
     * @apiSuccess {string}  sum_tokencount  总本金
     * @apiSuccess {string}  rationame  比率名称
     * @apiSuccess {string}  ratio  比率
     * @apiSuccess {string}  earnings_tokencount  总收益
     * @apiVersion 1.0.1
     */
    public function EarningsUserSum(){
        $phone=$this->param("phone");
        $billdate=$this->param("billdate");
        $sql="select r.guid roleguid,r.name rolename,count(*) counts, sum(a.sum_tokencount)/power(10,8) sum_tokencount,b.ratio,b.name rationame,sum(a.earnings_tokencount)/power(10,8) earnings_tokencount from token_bill_earnings a INNER JOIN token_sys_earnings_detail b on b.guid=a.token_sys_earnings_detail_guid  inner join token_sys_user c on a.token_sys_user_guid =c.guid
inner join token_sys_user d on a.friend_token_sys_user_guid =d.guid INNER JOIN token_sys_role r  on r.guid=d.token_sys_role_guid WHERE a.type=1 and c.phone='".$phone."' and a.billdate='".$billdate."' GROUP BY r.name,b.ratio,r.guid,b.name";
        $result=$this->db_select($sql);
        return $this->json_data($result,"");
    }


    /**
     *
     * @api {post} api/report/EarningsUserDetail  web_rep_收益_用户明细
     * @apiDescription   web_rep_用户收益明细
     * @apiGroup report
     * @apiParam {string} billdate 日期
     * @apiParam {string} phone 手机号
     * @apiParam {string} roleguid 角色guid
     *
     * @apiSuccess {string}  friend_phone  好友手机
     * @apiSuccess {string}  friend_role_name  好友角色
     * @apiSuccess {string}  sum_tokencount  本金
     * @apiSuccess {string}  rationame  比率名称
     * @apiSuccess {string}  ratio  比率
     * @apiSuccess {string}  earnings_tokencount  收益
     * @apiVersion 1.0.1
     */
    public function EarningsUserDetail(){
        $phone=$this->param("phone");
        $billdate=$this->param("billdate");
        $roleguid=$this->param("roleguid");
        $sql="select d.phone friend_phone,r.guid,r.name friend_role_name,a.sum_tokencount/power(10,8) sum_tokencount,b.ratio,b.name rationame,a.earnings_tokencount/power(10,8) earnings_tokencount from token_bill_earnings a INNER JOIN token_sys_earnings_detail b on b.guid=a.token_sys_earnings_detail_guid  inner join token_sys_user c on a.token_sys_user_guid =c.guid
inner join token_sys_user d on a.friend_token_sys_user_guid =d.guid INNER JOIN token_sys_role r  on r.guid=d.token_sys_role_guid WHERE a.type=1 and r.guid='".$roleguid."' and c.phone='".$phone."' and a.billdate='".$billdate."'";
        $result=$this->db_select($sql);
        return $this->json_data($result,"");
    }


    /**
     *
     * @api {post} api/report/GetUserS  web_rep_用户列表
     * @apiDescription   web_rep_用户列表
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} phone 手机号
     *
     * @apiSuccess {string}  phone 手机号
     * @apiSuccess {string}  name  角色
     * @apiSuccess {string}  token_count  代币数量
     * @apiSuccess {string}  isallow  禁用状态 0 禁用 1 允许
     * @apiVersion 1.0.1
     */
    public  function  GetUserS(){
        $page=$this->param("page");
        $count=$this->param("count");
        $phone=$this->param("phone");
        $sql="select  a.guid,a.phone,b.name,a.isallow,c.token_count/pow(10,8) token_count from token_sys_user a LEFT JOIN token_user_custom_coin_balance c on c.token_sys_user_guid=a.guid INNER JOIN token_sys_role b on a.token_sys_role_guid=b.guid 
WHERE a.issystem=0 and [选择条件]   ";
        if(strlen($phone)>0){
            $sql=str_replace("[选择条件]"," a.phone='".$phone."'",$sql);
        }
        $sql=str_replace("[选择条件]"," 1=1 ",$sql);
        $result =$this->SetReportPagination($sql,$page,$count);
        return $this->json_data($result,"");
    }

    /**
     *
     * @api {post} api/report/AppSumEarnings  App收益列表
     * @apiDescription   App收益列表
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiSuccess {string}  billdate  日期
     * @apiSuccess {string}  earnings_tokencount  总收益
     * @apiVersion 1.0.1
     */
    public  function  AppSumEarnings(){
        $page=$this->param("page");
        $count=$this->param("count");
        $userid=$this->getuserGuid();
        $sql="select  a.billdate,sum(a.earnings_tokencount)/power(10,8) earnings_tokencount from token_bill_earnings a WHERE  a.token_sys_user_guid='".$userid."'  GROUP BY a.billdate ORDER BY a.billdate desc LIMIT ";
        $sql=$sql.($page-1)*$count.",".$count."";
        return $this->json_data($this->db_select($sql),"");
    }



    /**
     *
     * @api {post} api/report/AppDetailEarnings  APP收益明细
     * @apiDescription    APP收益
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} billdate 日期
     * @apiSuccess {string}  system_earnings  系统收益 billdate 日期  sum_tokencount 代币总数量  earnings_tokencount 收益数量
     * @apiSuccess {string}  friend_earnings  好友收益 billdate 日期  earnings_tokencount 收益数量 friendphone 好友手机
     * @apiSuccess {string}  sum_earnings_tokencount  总收益
     * @apiSuccess {string}  coinname  代币名称
     * @apiVersion 1.0.1
     */
    public  function  AppDetailEarnings(){
        $page=$this->param("page");
        $count=$this->param("count");
        $billdate=$this->param("billdate");
        $userid=$this->getuserGuid();

        $row= $this->GetCoinInfo("1");
        $coinname=$row[0]["name"];

        $sql="select a.billdate,sum(a.earnings_tokencount/power(10,8)) earnings_tokencount from token_bill_earnings a  where a.billdate='".$billdate."' and a.token_sys_user_guid='".$userid."' group by a.billdate";
        $row=$this->db_select($sql);
        $sum_earnings_tokencount=$row[0]["earnings_tokencount"];

        $sql="select a.guid,a.billdate,a.sum_tokencount/power(10,8) sum_tokencount,a.earnings_tokencount/power(10,8) earnings_tokencount,c.ratio from token_bill_earnings a 
inner join token_sys_earnings_detail c on c.guid=a.token_sys_earnings_detail_guid  where a.billdate='".$billdate."' and  type=0 and a.token_sys_user_guid='".$userid."'";
        $row=$this->db_select($sql);


        $sql="select a.guid,a.billdate,a.earnings_tokencount/power(10,8) earnings_tokencount,d.phone friendphone from token_bill_earnings a 
 inner join token_sys_user d on d.guid=a.friend_token_sys_user_guid where type=1 and a.token_sys_user_guid='".$userid."' and a.billdate='".$billdate."' LIMIT ";
        $sql=$sql.($page-1)*$count.",".$count."";
        $detailRow=$this->db_select($sql);
        return $this->json_data(array("system_earnings"=>$row,"friend_earnings"=>$detailRow,"sum_earnings_tokencount"=>$sum_earnings_tokencount,"coinname"=>$coinname),"");
    }


    /**
     *
     * @api {post} api/report/ListRatio  兑换率报表
     * @apiDescription   兑换率报表
     * @apiGroup report
     * @apiParam {string} page 页 从1开始
     * @apiParam {string} count 每一页数量
     * @apiParam {string} billdate 日期  默认当天
     * @apiSuccess {string}  billdatetime  时间
     * @apiSuccess {string}  ratio  兑换率
     * @apiSuccess {string}  base_ratio  基准兑换率
     * @apiSuccess {string}  up_ratio  上浮率
     * @apiVersion 1.0.1
     */
    public function ListRatio(){
        $page=$this->param("page");
        $count=$this->param("count");
        $begindate=$this->param("begindate");
        $enddate=$this->param("enddate");
        $sql="select billdatetime,ratio,base_ratio,up_ratio from token_sys_ratio WHERE DATE_FORMAT(billdatetime,'%Y-%m-%d') between '".$begindate."' and '".$enddate."' ORDER BY billdatetime desc ";
        $result =$this->SetReportPagination($sql,$page,$count);
        return $this->json_data($result,"");
    }


    /**
     *
     * @api {post} api/report/TradViewMM  TrD按分钟
     * @apiDescription   TrD按分钟
     * @apiGroup report
     * @apiParam {string} start_datestamp 开始时间戳
     * @apiParam {string} end_datestamp 结束时间戳
     * @apiVersion 1.0.1
     */
    public  function  TradViewMM(){
        $startdate=$this->param("start_datestamp");
        $enddate=$this->param("end_datestamp");
//        $ddate="[{\"id\":1567405140,\"open\":9747.100000000000000000,\"close\":9746.260000000000000000,\"low\":9746.200000000000000000,\"high\":9747.300000000000000000,\"amount\":1.008426350617106626,\"vol\":9829.110644759999994284600000000000000000,\"count\":18},{\"id\":1567405020,\"open\":9746.390000000000000000,\"close\":9746.460000000000000000,\"low\":9743.300000000000000000,\"high\":9747.000000000000000000,\"amount\":4.118428640321560040,\"vol\":40133.089192295275129862200000000000000000,\"count\":96},{\"id\":1567404960,\"open\":9744.620000000000000000,\"close\":9747.000000000000000000,\"low\":9744.170000000000000000,\"high\":9747.210000000000000000,\"amount\":2.808556949079686052,\"vol\":27368.641910199999965493930000000000000000,\"count\":64},{\"id\":1567404900,\"open\":9747.830000000000000000,\"close\":9744.630000000000000000,\"low\":9744.610000000000000000,\"high\":9747.900000000000000000,\"amount\":7.376797862567383643,\"vol\":71898.430995419999945149510000000000000000,\"count\":111},{\"id\":1567404840,\"open\":9745.250000000000000000,\"close\":9747.840000000000000000,\"low\":9745.210000000000000000,\"high\":9748.250000000000000000,\"amount\":3.837728049752028722,\"vol\":37405.990772427918514317430000000000000000,\"count\":108},{\"id\":1567404780,\"open\":9743.570000000000000000,\"close\":9745.100000000000000000,\"low\":9743.500000000000000000,\"high\":9745.290000000000000000,\"amount\":6.835892723723095004,\"vol\":66613.017159494945168293090000000000000000,\"count\":67},{\"id\":1567404720,\"open\":9744.980000000000000000,\"close\":9744.000000000000000000,\"low\":9742.700000000000000000,\"high\":9745.000000000000000000,\"amount\":6.354368042058443999,\"vol\":61917.278166222495864889800000000000000000,\"count\":60},{\"id\":1567404660,\"open\":9744.400000000000000000,\"close\":9745.000000000000000000,\"low\":9742.670000000000000000,\"high\":9745.000000000000000000,\"amount\":6.558138703855310413,\"vol\":63907.850581557203069700500000000000000000,\"count\":65},{\"id\":1567404600,\"open\":9744.700000000000000000,\"close\":9744.270000000000000000,\"low\":9742.850000000000000000,\"high\":9745.000000000000000000,\"amount\":2.654887935539615192,\"vol\":25869.326377799999984076600000000000000000,\"count\":60},{\"id\":1567404540,\"open\":9745.030000000000000000,\"close\":9744.700000000000000000,\"low\":9743.750000000000000000,\"high\":9745.930000000000000000,\"amount\":2.200371110865922543,\"vol\":21441.743534439999965187300000000000000000,\"count\":90},{\"id\":1567404480,\"open\":9747.000000000000000000,\"close\":9745.930000000000000000,\"low\":9743.870000000000000000,\"high\":9747.000000000000000000,\"amount\":3.469609407917090028,\"vol\":33813.270544595324241916180000000000000000,\"count\":104},{\"id\":1567404420,\"open\":9746.900000000000000000,\"close\":9746.510000000000000000,\"low\":9746.000000000000000000,\"high\":9747.000000000000000000,\"amount\":6.249971402072432541,\"vol\":60916.940290729999977127000000000000000000,\"count\":60},{\"id\":1567404360,\"open\":9746.000000000000000000,\"close\":9747.000000000000000000,\"low\":9745.820000000000000000,\"high\":9747.900000000000000000,\"amount\":2.817692323041333274,\"vol\":27462.520830609999985601000000000000000000,\"count\":65},{\"id\":1567404300,\"open\":9745.720000000000000000,\"close\":9746.000000000000000000,\"low\":9745.000000000000000000,\"high\":9746.000000000000000000,\"amount\":8.154261084115986823,\"vol\":79464.192830064340527318440000000000000000,\"count\":62},{\"id\":1567404240,\"open\":9746.000000000000000000,\"close\":9745.840000000000000000,\"low\":9745.090000000000000000,\"high\":9746.000000000000000000,\"amount\":3.796120099744424327,\"vol\":36996.435615739999972624560000000000000000,\"count\":42},{\"id\":1567404180,\"open\":9745.750000000000000000,\"close\":9745.990000000000000000,\"low\":9745.700000000000000000,\"high\":9746.000000000000000000,\"amount\":2.210070434914470348,\"vol\":21539.119848819999975203000000000000000000,\"count\":50},{\"id\":1567404120,\"open\":9745.810000000000000000,\"close\":9746.000000000000000000,\"low\":9744.810000000000000000,\"high\":9748.300000000000000000,\"amount\":4.250747199410529663,\"vol\":41427.821759739999983735600000000000000000,\"count\":89},{\"id\":1567404060,\"open\":9745.000000000000000000,\"close\":9745.810000000000000000,\"low\":9744.640000000000000000,\"high\":9750.940000000000000000,\"amount\":9.411632150159554570,\"vol\":91743.043666823356308666020000000000000000,\"count\":146},{\"id\":1567404000,\"open\":9745.300000000000000000,\"close\":9745.000000000000000000,\"low\":9745.000000000000000000,\"high\":9745.620000000000000000,\"amount\":6.060729400214483613,\"vol\":59063.560179419999950439460000000000000000,\"count\":113}]";
//        $json=json_decode($ddate);
//        return $this->json_data($json,"");

        $row=$this->db_select("select timestamp_id id,amount,count,open,close,low,high,vol from token_bill_mm_report ORDER BY billdatetime DESC LIMIT 0,200");
      //  $row=$this->db_select("select timestamp_id id,amount,count,open,close,low,high,vol from token_bill_mm_report where timestamp_id between ".$startdate." and ".$enddate." ORDER BY billdatetime ");
        return $this->json_data($row,"");
    }

    /**
     *
     * @api {post} api/report/TradViewDD  TrD按天
     * @apiDescription   TrD按天
     * @apiGroup report
     * @apiParam {string} start_datestamp 开始时间戳
     * @apiParam {string} end_datestamp 结束时间戳
     * @apiVersion 1.0.1
     */
    public  function  TradViewDD(){
        $startdate=$this->param("start_datestamp");
        $enddate=$this->param("end_datestamp");
        $row=$this->db_select("select timestamp_id id,amount,count,open,close,low,high,vol from token_bill_dd_report ORDER BY billdate DESC LIMIT 0,200");
        return $this->json_data($row,"");
    }

    /**
     *
     * @api {post} api/report/TradViewWW  TrD按周
     * @apiDescription   TrD按周
     * @apiGroup report
     * @apiParam {string} start_datestamp 开始时间戳
     * @apiParam {string} end_datestamp 结束时间戳
     * @apiVersion 1.0.1
     */
    public  function  TradViewWW(){
        $startdate=$this->param("start_datestamp");
        $enddate=$this->param("end_datestamp");
        $row=$this->db_select("select timestamp_id id,amount,count,open,close,low,high,vol from token_bill_ww_report ORDER BY billdate DESC LIMIT 0,200");
        return $this->json_data($row,"");
    }



}